<?php
namespace app\common\netfilter;

class Windows2008Rule
{
    public $rulehead = 'netsh advfirewall firewall';//规则头部
    private $status ;//规则状态
    private $direction ;//流量方向
    private $protocols;//协议
    /**
     * @name: 状态
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:43:45
     */
    function status($status)
    {
        $this->status = $status;
        $return = $status == 1 ? 'add' : 'delete';
        $return .= ' rule';

        return $return;
    }

    /**
     * @name: 规则名
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:46:28
     */
    function alias ($alias)
    {
        $return = 'name="' . $alias . '"';
        return $return;
    }

    /**
     * @name: 流量方向
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:49:12
     */
    function direction ($direction)
    {
        $this->direction = $direction;
        $return = '';
        if ($this->status != 1)
        {
            return $return;
        }
        $return .= 'dir=';
        $return .= $direction == 1 ? 'out' : 'in';
        return $return;
    }

    /**
     * @name: 处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:50:24
     */
    function process ($process)
    {
        $return = '';
        if ($this->status != 1)
        {
            return $return;
        }

        $return .= 'action=';
        $return .= $process == 1 ? 'allow' : 'block';
        return $return;
    }

    /**
     * @name: 协议
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:52:04
     */
    function protocols ($protocols)
    {
        $this->protocols = $protocols;
        $return = '';
        if ($protocols == 3 || $this->status != 1)
        {
            return $return;
        }

        $return = 'protocol=';
        $return .= $protocols == 1 ? 'tcp' : 'udp';
        return $return;
    }

    /**
     * @name: ip地址
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:52:44
     */
    function ip ($src_ip, $dest_ip)
    {
        $return = '';

        if ($this->status != 1)
        {
            return $return;
        }

        if($this->direction == 1)
        {
            $localip = $src_ip;
            $remoteip = $dest_ip;
        }
        else
        {
            $localip = $dest_ip;
            $remoteip = $src_ip;
        }
        

        $return .= $localip == 'any' ? '' : 'localip=' . $localip . ' ';
        $return .= $remoteip == 'any' ? '' : 'remoteip=' . $remoteip;
        
        return $return;
    }

    /**
     * @name: 端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-14 11:17:46
     */
    function port ($src_port, $dest_port)
    {
        $return = '';

        if ( $this->protocols == 3 || $this->status != 1)
        {
            return $return;
        }
        $dest_port = str_replace(':', '-', $dest_port);
        $src_port = str_replace(':', '-', $src_port);
        if ($this->direction == 1)
        {
            $remoteport = $dest_port;
            $localport = $src_port;
        }
        else
        {
            $remoteport = $src_port;
            $localport = $dest_port;
        }
        

        $return .= $localport == 'any' ? '' : 'localport=' . $localport;
        $return .= $remoteport == 'any' ? '' : 'remoteport=' . $remoteport . ' ';

        return $return;
    }

}